package com.gaoda.booksystem.service.impl;

import com.gaoda.booksystem.entity.Book;
import com.gaoda.booksystem.entity.Borrowing;
import com.gaoda.booksystem.entity.Revert;
import com.gaoda.booksystem.entity.User;
import com.gaoda.booksystem.mapper.UserMapper;
import com.gaoda.booksystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: LuckyBear
 * @date: 2018-12-12 10:06
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public User findUserByName(String username) {
        return userMapper.findUserByName(username);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void updatePwd(User user) {
        userMapper.updatePwd(user);
    }

    @Override
    public Book findBookByNumber(String bookNumber) {
        return userMapper.findBookByNumber(bookNumber);
    }


    @Override
    public List<Book> findBooks(String bookName, String bookPublishers, String bookType, int currentPage, int pageCount) {
        List<Book> books = userMapper.findBooks(bookName, bookPublishers, bookType, (currentPage - 1) * pageCount, pageCount*currentPage);
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i).getBrief().length() > 200) {
                books.get(i).setBrief(books.get(i).getBrief().substring(0, 200) + "......");
            }
        }

        return books;
    }

    @Override
    public int getTotal(int pageCount, String bookName, String bookPublishers, String bookType) {
        Integer total = userMapper.getTotal(bookName, bookPublishers, bookType);
        return (total - 1) / pageCount + 1;
    }

    @Override
    public List<Borrowing> myBooks(String userNumber) {
        return userMapper.myBooks(userNumber);
    }


    @Override
    public int borrowingBook(Borrowing borrowing) {
        return userMapper.borrowingBook(borrowing);
    }

    @Override
    public void updateBookNumLess(String bookNumber) {
        userMapper.updateBookNumLess(bookNumber);
    }

    @Override
    public int revertBook(Revert revert) {
        return userMapper.revertBook(revert);
    }

    @Override
    public void updateBookFlag(Borrowing borrowing) {
        userMapper.updateBookFlag(borrowing);
    }

    @Override
    public void updateBookNumMore(String bookNumber) {
        userMapper.updateBookNumMore(bookNumber);
    }

    @Override
    public List<Borrowing> findBorrowing(Borrowing borrowing) {
        return userMapper.findBorrowing(borrowing);
    }

    @Override
    public List<Borrowing> findBorrowingBook(Borrowing borrowing) {
        return userMapper.findBorrowingBook(borrowing);
    }

    @Override
    public List<Borrowing> queryBorrowing(String bookNumber) {
        return userMapper.queryBorrowing(bookNumber);
    }

    @Override
    public List<Book> AllBook() {
        return userMapper.AllBook();
    }
}
